package com.lagou.rpc.consumer;

import com.lagou.curator.ServerRegistryCenter;
import com.lagou.rpc.api.IUserService;
import com.lagou.rpc.consumer.proxy.RpcClientProxy;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: ServerBootstrapApplication
 * @Description:
 * @Author: qjc
 * @Date: 2021/9/21 10:45
 */
@SpringBootApplication
public class ClientBootstrapApplication {

    public static List<String> childrenList = new ArrayList<>();
    public static ServerRegistryCenter serverRegistryCenter;

    public static void main(String[] args) {
        SpringApplication.run(ClientBootstrapApplication.class, args);
        try {
            runTimeTask();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void runTimeTask() throws Exception {
        serverRegistryCenter = new ServerRegistryCenter();
        childrenList = serverRegistryCenter.getChildren(ServerRegistryCenter.RPC_ROOT);
        serverRegistryCenter.registryWatch(ServerRegistryCenter.RPC_ROOT, childrenList);
        ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(10);
        scheduledExecutorService.scheduleAtFixedRate(() -> {
            childrenList = serverRegistryCenter.getChildren(ServerRegistryCenter.RPC_ROOT);
            for (String childrenPath : childrenList) {
                String childrenNodeData = serverRegistryCenter.getChildrenNodeData(childrenPath);
                if (childrenNodeData != null) {
                    String[] split = childrenNodeData.split(ServerRegistryCenter.RPC_COLON);
                    String ip = split[0];
                    int port = Integer.parseInt(split[1]);
                    String lastTime = null;
                    if (split.length == 4) {
                        lastTime = split[3];
                        long l = Long.valueOf(lastTime);
                        long nowTimeMillis = System.currentTimeMillis();
                        if (nowTimeMillis - l > 10000) {
                            System.out.println("节点" + childrenPath + "的value：" + childrenNodeData + "，已经" + (nowTimeMillis - l) + "ms未访问，设置为失效");
                            String value = ip + ServerRegistryCenter.RPC_COLON + port;
                            serverRegistryCenter.updateChildrenNodeData(childrenPath, value);
                            continue;
                        }
                    }
                    if (split.length == 3 || split.length == 4) {
                        long start = System.currentTimeMillis();
                        IUserService userService = (IUserService) RpcClientProxy.createProxy(IUserService.class, ip, port);
                        userService.getById(1, true);
                        long end = System.currentTimeMillis();
                        String time = dealTime(String.valueOf((end - start)));
                        String value;
                        if (lastTime != null) {
                            value = ip + ServerRegistryCenter.RPC_COLON + port + ServerRegistryCenter.RPC_COLON + time + ServerRegistryCenter.RPC_COLON + lastTime;
                        } else {
                            value = ip + ServerRegistryCenter.RPC_COLON + port + ServerRegistryCenter.RPC_COLON + time;
                        }
                        serverRegistryCenter.updateChildrenNodeData(childrenPath, value);
                        System.out.println("--------节点：" + childrenPath + "的value：" + childrenNodeData + "，最新value：" + value);
                    }
                    if (split.length == 2) {
                        System.out.println("--------节点：" + childrenPath + "的value：" + childrenNodeData + "，已失效");
                    }
                }
            }
            System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        }, 0, 10, TimeUnit.SECONDS);
    }

    private static String dealTime(String valueOf) {
        return valueOf.substring(0, valueOf.length() - 1) + "0";
    }
}
